package io.unicorn.modules.water.controller;

import com.alibaba.fastjson.JSONObject;
import io.unicorn.common.utils.ExcelUtils;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.R;
import io.unicorn.common.validator.ValidatorUtils;
import io.unicorn.modules.sys.entity.SysDeptEntity;
import io.unicorn.modules.sys.entity.SysUserEntity;
import io.unicorn.modules.sys.service.SysDeptService;
import io.unicorn.modules.sys.shiro.ShiroUtils;
import io.unicorn.modules.water.service.DataErrAlertFixedService;
import io.unicorn.modules.waterfgs.entity.DataErrAlertFixedFgsEntity;
import io.unicorn.modules.waterfgs.service.DataErrAlertFixedFgsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * ${comments}
 *
 * @author LNCG
 * @email LNCG@USTL.EDU.CN
 * @date 2023-04-03 16:49:58
 */
@RestController
@RequestMapping("water/dataerralertfixed")
public class DataErrAlertFixedController {
    @Autowired
    private DataErrAlertFixedService dataErrAlertFixedService;

    @Autowired
    private SysDeptService sysDeptService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("water:dataerrfixedinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dataErrAlertFixedService.queryPage(params);

        return R.ok().put("page", page);
    }

    /*子部门信息*/
    @RequestMapping("/listchart")
    @RequiresPermissions("water:dataerrfixedinfo:list")
    public R listChart(@RequestParam Map<String, Object> params){

        //SysUserEntity user = ShiroUtils.getUserEntity();
        //SysDeptEntity dept = sysDeptService.getById(user.getDeptId());
        //Long deptId =  Long.parseLong(dept.getOrderNum().toString());
        //params.put("unitId",deptId);

        PageUtils page = dataErrAlertFixedService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{opKey}")
    @RequiresPermissions("water:dataerrfixedinfo:info")
    public R info(@PathVariable("opKey") Integer opKey){
        DataErrAlertFixedFgsEntity dataErrAlertFixed = dataErrAlertFixedService.getById(opKey);

        return R.ok().put("dataErrAlertFixed", dataErrAlertFixed);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("water:dataerrfixedinfo:save")
    public R save(@RequestBody DataErrAlertFixedFgsEntity dataErrAlertFixed){
        dataErrAlertFixedService.save(dataErrAlertFixed);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("water:dataerrfixedinfo:update")
    public R update(@RequestBody DataErrAlertFixedFgsEntity dataErrAlertFixed){
        ValidatorUtils.validateEntity(dataErrAlertFixed);
        dataErrAlertFixedService.updateById(dataErrAlertFixed);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("water:dataerrfixedinfo:delete")
    public R delete(@RequestBody Integer[] opKeys){
        dataErrAlertFixedService.removeByIds(Arrays.asList(opKeys));

        return R.ok();
    }

    @RequestMapping("/export")
    @RequiresPermissions("water:dataerrfixedinfo:list")
    public void export(@RequestParam Map<String, Object> params,
                       HttpServletRequest request,
                       HttpServletResponse repsonse){

        JSONObject jsonObject = JSONObject.parseObject((String)params.get("params"));

        //SysUserEntity user = ShiroUtils.getUserEntity();
        //SysDeptEntity dept = sysDeptService.getById(user.getDeptId());
        //Long deptId =  Long.parseLong(dept.getOrderNum().toString());
        //jsonObject.put("unitId",deptId.toString());

        //System.out.println(jsonObject);

        PageUtils page = dataErrAlertFixedService.queryPage(jsonObject);
        List<?> listEntity = page.getList();

        ExcelUtils.exportXlsx(
                repsonse,
                jsonObject.get("dept").toString(),
                "io.unicorn.modules.waterfgs.entity.DataErrAlertFixedEntity",
                listEntity);
    }
}
